<?php
$amount = isset($_GET['amount']) ? $_GET['amount'] : 1000;
$siz = isset($_GET['s']) ? $_GET['s'] : 100;
$ap = isset($_GET['a']) ? $_GET['a'] : '0-0.5';
$bp = isset($_GET['b']) ? $_GET['b'] : '0-0.5';

$aps = trans(explode(',',$ap));


$bps = trans(explode(',',$bp));

$_ap = isset($_GET['ap']) ? $_GET['ap'] : 0.3;
echo "<pre>";

$s = json_encode(gl($siz,$amount,$aps,$bps,$_ap));
$x = range(0,$siz);


function trans($aps)
{
    $naps = [];
    foreach ($aps as $_k=>$_i)
    {
        $_t = explode('-',$_i);
        $naps[$_t[0]] = $_t[1];
    }
    return $naps;
}

function gl($size=10,$amount,$aps=0.5,$bps=0.6,$_ap)
{
    $a_per = $amount*$_ap;
    $b_per = $amount*(1-$_ap);

    $a_list = kk($size,$aps);
    echo "<pre>";
    //var_dump($a_list);
    $b_list = kk($size,$bps);
    $len = count($a_list);

    $s = [];
    foreach ($a_list as $k=>$v)
    {
        //var_dump("{$v}*{$a_per}+{$b_list[$len-1-$k]}*{$b_per}");exit;
        $s[$k] = round($v*$a_per+$b_list[$len-1-$k]*$b_per,2);
        //echo round($v*$a_per+$b_list[$len-1-$k]*$b_per,2).','.($k+1);echo "<br>";
        echo round($v*$a_per+$b_list[$len-1-$k]*$b_per,2);echo ",";

    }
    return $s;

}

function kk($size=10,$pers)
{
    $per_total = 0;
    $item_pers = [];
    $prev = 1;
    $kkk = 0;
    $csize = $size;


    foreach($pers as $k=>$per)
    {
        //echo "<br>";
        if($k==0) $k = $csize ;
        for($i=0;$i<$k;$i++)
        {
            $per_total += $prev;
            $item_pers[] = $prev;
            $prev = $prev*$per;
        }
        $csize = $size - $k;
    }

    $item = $per_total ? 1/$per_total : 0;

    //echo 'per_total';var_dump($per_total);
    //var_dump($item_pers);
    $list = [];
    foreach ($item_pers as $per)
    {
        $list[] = $item*$per;
    }

    //echo 'list:';var_dump($list);

    return $list;
}

?>

<form >
    总金额：<input type="text" name="amount" value="<?php echo $amount ? $amount : 1000;?>"><br>
    用户数：<input type="text" name="s" value="<?php echo $siz;?>"><br>
    从发起端至自荐者递减比率：<input type="text" name="a" value="<?php echo $ap;?>"><br>
    从自荐者至发起端递减比率：<input type="text" name="b" value="<?php echo $bp;?>"><br>
    发起端->自荐者 占整体分配比例：<input type="text" name="ap" value="<?php echo $_ap;?>"><br>
    <input type="submit" value="提交">
</form>


<script src="http://cdn.bootcss.com/echarts/4.1.0.rc2/echarts-en.common.js"></script>

<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
    var s= "<?php echo $s;?>";
    var x= "<?php echo json_encode($x);?>";

    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));

    // 指定图表的配置项和数据
    var option = {
        xAxis: {
            type: 'category',
            data:  JSON.parse(x)
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: JSON.parse(s),
            type: 'line',
            smooth: true
        }]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
</script>



